package com.jf.blog.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

public class DateRangeUtils {

    // 定义格式化器
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 获取今天的开始和结束时间
     */
    public static String[] getToday() {
        LocalDate today = LocalDate.now();
        LocalDateTime startOfDay = today.atStartOfDay(); // 今天 00:00:00
        LocalDateTime endOfDay = today.plusDays(1).atStartOfDay(); // 明天 00:00:00 (作为今天的结束)

        System.out.println("=== 今天 ===");
        System.out.println("开始时间: " + startOfDay.format(FORMATTER));
        System.out.println("结束时间: " + endOfDay.minusNanos(1).format(FORMATTER)); // 或者直接打印 endOfDay 并注明是开区间
        return new String[]{startOfDay.format(FORMATTER), endOfDay.minusNanos(1).format(FORMATTER)};
    }

    /**
     * 获取最近一周的开始和结束时间
     */
    public static String[] getLastWeek() {
        LocalDate today = LocalDate.now();
        // 最近7天：从7天前到今天
        LocalDate weekAgo = today.minusDays(6); // 如果包含今天，则往前推6天
        LocalDateTime startOfWeek = weekAgo.atStartOfDay(); // 7天前 00:00:00
        LocalDateTime endOfWeek = today.plusDays(1).atStartOfDay(); // 今天明天 00:00:00

        System.out.println("\n=== 最近一周 (过去7天) ===");
        System.out.println("开始时间: " + startOfWeek.format(FORMATTER));
        System.out.println("结束时间: " + endOfWeek.minusNanos(1).format(FORMATTER));
        return new String[]{startOfWeek.format(FORMATTER), endOfWeek.minusNanos(1).format(FORMATTER)};
    }

    /**
     * 获取最近一个月的开始和结束时间
     */
    public static String[] getLastMonth() {
        LocalDate today = LocalDate.now();
        LocalDate monthAgo = today.minusMonths(1).plusDays(1); // 上个月的同一天 + 1天
        LocalDateTime startOfMonth = monthAgo.atStartOfDay();
        LocalDateTime endOfMonth = today.plusDays(1).atStartOfDay();

        System.out.println("\n=== 最近一个月 ===");
        System.out.println("开始时间: " + startOfMonth.format(FORMATTER));
        System.out.println("结束时间: " + endOfMonth.minusNanos(1).format(FORMATTER));
        return new String[]{startOfMonth.format(FORMATTER), endOfMonth.minusNanos(1).format(FORMATTER)};
    }

    /**
     * 获取最近三个月的开始和结束时间
     */
    public static String[] getLastThreeMonths() {
        LocalDate today = LocalDate.now();
        LocalDate threeMonthsAgo = today.minusMonths(3).plusDays(1);
        LocalDateTime startOfThreeMonths = threeMonthsAgo.atStartOfDay();
        LocalDateTime endOfThreeMonths = today.plusDays(1).atStartOfDay();

        System.out.println("\n=== 最近三个月 ===");
        System.out.println("开始时间: " + startOfThreeMonths.format(FORMATTER));
        System.out.println("结束时间: " + endOfThreeMonths.minusNanos(1).format(FORMATTER));
        return new String[]{startOfThreeMonths.format(FORMATTER), endOfThreeMonths.minusNanos(1).format(FORMATTER)};
    }

    /**
     * 获取最近半年的开始和结束时间
     */
    public static String[] getLastHalfYear() {
        LocalDate today = LocalDate.now();
        LocalDate halfYearAgo = today.minusMonths(6).plusDays(1);
        LocalDateTime startOfHalfYear = halfYearAgo.atStartOfDay();
        LocalDateTime endOfHalfYear = today.plusDays(1).atStartOfDay();

        System.out.println("\n=== 最近半年 ===");
        System.out.println("开始时间: " + startOfHalfYear.format(FORMATTER));
        System.out.println("结束时间: " + endOfHalfYear.minusNanos(1).format(FORMATTER));
        return new String[]{startOfHalfYear.format(FORMATTER), endOfHalfYear.minusNanos(1).format(FORMATTER)};
    }

    /**
     * 获取最近一年的开始和结束时间
     */
    public static String[] getLastYear() {
        LocalDate today = LocalDate.now();
        LocalDate yearAgo = today.minusYears(1).plusDays(1);
        LocalDateTime startOfYear = yearAgo.atStartOfDay();
        LocalDateTime endOfYear = today.plusDays(1).atStartOfDay();

        System.out.println("\n=== 最近一年 ===");
        System.out.println("开始时间: " + startOfYear.format(FORMATTER));
        System.out.println("结束时间: " + endOfYear.minusNanos(1).format(FORMATTER));
        return new String[]{startOfYear.format(FORMATTER), endOfYear.minusNanos(1).format(FORMATTER)};
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(getToday()));
        System.out.println(Arrays.toString(getLastWeek()));
        System.out.println(Arrays.toString(getLastMonth()));
        System.out.println(Arrays.toString(getLastThreeMonths()));
        System.out.println(Arrays.toString(getLastHalfYear()));
        System.out.println(Arrays.toString(getLastYear()));
    }
}
